I have a npm package called "mynpmpackage" . I have a file called index.js that has this content:
export default const Sum = (a , b) => {
return a + b
}
package.json :
{
"name":"mynpmpackage",
"version":"1.0.0",
"main": "index.js",
"module": "index.js"
}
I published this package with npm publish and It published correctly .
I created another directory , and Installed my package with npm install .
Then , I made an index.html file :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script type="module" src="test.js"></script>
</html>
in test.js :
import Sum from "mynpmpackage"
alert(Sum(3,4))
but it gave this Error :
Uncaught TypeError: Failed to resolve module specifier "mynpmpackage". Relative references must start with either "/", "./", or "../".
Just replace your package name with ./node_modules/mynpmpackage/index.js
It really works!